package org.ceteca.explica.client.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class EntityExtensionCatalogListener implements Listener {
	private Tree tree;
	private Color black;
	private TreeEditor editor;
	private TreeItem [] lastItem;
	
	public EntityExtensionCatalogListener(Tree tree) {
		this.tree = tree;
		this.black = ExplicaGUIBuilder.getInstance().getColor(SWT.COLOR_BLACK);
		this.editor = new TreeEditor(this.tree);
		this.lastItem = new TreeItem [1];
	}
	
	public void handleEvent (Event event) {
		final TreeItem item = (TreeItem) event.item;
		if (item != null && item == lastItem [0]) {
			boolean showBorder = true;
			final Composite composite = new Composite (tree, SWT.NONE);
			if (showBorder) composite.setBackground (black);
			final Text text = new Text (composite, SWT.NONE);
			final int inset = showBorder ? 1 : 0;
			
			EntityExtensionCatalogResizeListener resListener = new EntityExtensionCatalogResizeListener(composite, text, inset);
			composite.addListener (SWT.Resize, resListener);

			EntityExtensionCatalogTextListener textListener = new EntityExtensionCatalogTextListener(tree, item, composite, text, editor, inset);
			
			text.addListener (SWT.FocusOut, textListener);
			text.addListener (SWT.Traverse, textListener);
			text.addListener (SWT.Verify, textListener);
			editor.setItem(item);
			editor.setColumn(1);
			editor.setEditor(composite);
//			editor.setEditor (composite, item);
			text.setText (item.getText (1));
			text.selectAll ();
			text.setFocus ();
		}
		lastItem [0] = item;
	}
}
